package com.bdqn.tour.servlet.Hzy;

import com.alibaba.fastjson.JSONObject;
import com.bdqn.tour.pojo.HzyOrder;
import com.bdqn.tour.service.impl.HzyOrdersServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/orders")
public class OrdersServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=utf-8");

        String action = req.getParameter("action");

        JSONObject result = new JSONObject();

        HzyOrdersServiceImpl ordersService = new HzyOrdersServiceImpl();
        List<HzyOrder> orders;

        switch (action) {
            case "hotelOrders":
                orders = ordersService.getAllHotelOrders();
                break;
            case "scenicOrders":
                orders = ordersService.getAllScenicOrders();
                break;
            case "ticketOrders":
                orders = ordersService.getAllTicketOrders();
                break;
            default:
                orders = ordersService.getAllOrders();

        }

        result.put("success", true);
        result.put("data", orders);

        String jsonResponse = result.toJSONString();
        resp.getWriter().write(jsonResponse);
    }
}
